/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src;

import java.util.*;
import beans.*;
import java.io.*;

/**
 *
 * @author cyhaberl
 */
public class PlanningSemaine {
	private int semaine;
	private int formation;
	private int promotion;
	private int jour;
	private int mois;
	private int annee;
	public List<PlanningJour> tabSemaine;

	public PlanningSemaine(int jour,int mois,int annee, int formation, int promotion){
		//gerer semaine sur deux mois
		super();
		this.formation = formation;
		this.promotion = promotion;
		this.remplir(jour,mois,annee);
	}

	public final void remplir(int jour, int mois, int annee){
		long MILLISECONDS_PER_DAY = 1000*60*60*24;
		Calendar monCal = Calendar.getInstance();
		monCal.set(annee, (mois-1), jour);
		//monCal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);

		//On trouve le premier jour de la semaine.
		//monCal.set(Calendar.DAY_OF_WEEK,monCal.getActualMinimum(Calendar.DAY_OF_WEEK));
		//System.out.println(monCal.get(Calendar.DATE)+"/"+(monCal.get(Calendar.MONTH)+1)+"/"+monCal.get(Calendar.YEAR)+" - day of week :"+monCal.get(Calendar.DAY_OF_WEEK));
		switch(monCal.get(Calendar.DAY_OF_WEEK))
		{
			case 1: monCal.setTimeInMillis(monCal.getTimeInMillis()-(6*MILLISECONDS_PER_DAY));
				break;
			case 3: monCal.setTimeInMillis(monCal.getTimeInMillis()-MILLISECONDS_PER_DAY);
				break;
			case 4: monCal.setTimeInMillis(monCal.getTimeInMillis()-(2*MILLISECONDS_PER_DAY));
				break;
			case 5: monCal.setTimeInMillis(monCal.getTimeInMillis()-(3*MILLISECONDS_PER_DAY));
				break;
			case 6: monCal.setTimeInMillis(monCal.getTimeInMillis()-(4*MILLISECONDS_PER_DAY));
				break;
			case 7: monCal.setTimeInMillis(monCal.getTimeInMillis()-(5*MILLISECONDS_PER_DAY));
				break;
		}
		//System.out.println("day of week 2 :"+monCal.get(Calendar.MONTH));
		/*if(monCal.get(Calendar.DAY_OF_WEEK)>5)
		{
			monCal.setTimeInMillis(monCal.getTimeInMillis()-((monCal.get(Calendar.DAY_OF_WEEK)-1)*MILLISECONDS_PER_DAY));
		}
		else
		{
			if(monCal.get(Calendar.DAY_OF_WEEK)==4)
			{
				monCal.setTimeInMillis(monCal.getTimeInMillis()+MILLISECONDS_PER_DAY);
				System.out.println("day 2 :"+monCal.get(Calendar.DAY_OF_WEEK));
			}
		}*/

		this.jour = monCal.get(Calendar.DATE);
		this.mois = monCal.get(Calendar.MONTH);
		this.annee = monCal.get(Calendar.YEAR);
		this.tabSemaine = new ArrayList();
		for (int i=0;i<6;i++)
		{
			//System.out.println(monCal.get(Calendar.DATE)+"/"+(monCal.get(Calendar.MONTH)+1)+"/"+monCal.get(Calendar.YEAR));
			this.tabSemaine.add(new PlanningJour(monCal.get(Calendar.DATE),(monCal.get(Calendar.MONTH)+1),monCal.get(Calendar.YEAR),formation,promotion));
			monCal.setTimeInMillis(monCal.getTimeInMillis()+MILLISECONDS_PER_DAY);
		}
	}

	public String afficherSemaine()
	{
		StringBuilder planning = new StringBuilder();
		int[] rowspan = new int[6];
		boolean memeCase = true;
		String crenCourant = null;
		int cren = 0;
		int i;
		/*StringBuilder  debug = new StringBuilder();
		debug.append("<script>");*/

		//Si changement de border : c'est ici
		planning.append("Semaine du ").append(this.jour).append("/").append((this.mois+1)).append("/").append(this.annee);
		planning.append("<table border=1 class='EDTsemaine'><tr><th>Horaires</th><th>Lundi</th><th>Mardi</th><th>Mercredi</th><th>Jeudi</th><th>Vendredi</th><th>Samedi</th></tr>");
		for(i = 0; i<6; i++){
				rowspan[i]=0;
		}

		// Pour chaque créneau
		for(i=0; i<48; i++)
		{
			planning.append("<tr class='ligneCreneau' height=25>"); //ligne
			if((i%4) == 0)
			{
				planning.append("<td rowspan=4 class='cellHoraire'>");
				planning.append(i/4+7);
				planning.append(":00</td>");
			}

			// Pour chaque jour
			for(int j = 0; j <6; j++)
			{
				//debug.append("console.log(' rowspan "+j+"  = "+rowspan[j]+" i="+i+"');");
				// Aucun créneau en cours
				if(rowspan[j]<=0)
				{
					cren = i; //ligne en cours
					memeCase = true;
					crenCourant = this.tabSemaine.get(j).creneau.get(cren);
					while(memeCase)
					{
						crenCourant = this.tabSemaine.get(j).creneau.get(cren);
						if(crenCourant.compareTo("") != 0)
						{
							if(cren<47 && crenCourant.compareTo(this.tabSemaine.get(j).creneau.get(cren+1))==0)
							{
								rowspan[j]++;
								cren++;
							}
							else
							{
								memeCase = false;
								cren++;
							}
						}
						else
						{
							memeCase = false;
							crenCourant = "&nbsp;";
						}
					}
					planning.append("<td rowspan=\"");
					planning.append((rowspan[j]+1));
					planning.append("\" class='cellCours'>");
					planning.append(crenCourant);
					planning.append("</td>");
				}
				else
				{
					rowspan[j]--;
				}
			}
			planning.append("</tr>");
		}
		planning.append("</table>");

		//debug.append("</script>");
		//planning.append(debug);
		return planning.toString();
	}
	
	
}
